﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Drawing;
using System.Globalization;

namespace LIB
{
    public class Utils
    {
        /// <summary>
        /// Author: minhdv1
        /// </summary>
        /// <returns></returns>
        /// 
        public static string FieldTemp;
        public static string FieldTemp1;
        public static string GroupFilterValue;
        public static string GroupFilterText;
        public static string GroupFilterID;

        #region GencodeRandom

        public static string GenerateRandomCode()
        {
            Random random = new Random();

            string s = "";
            for (int i = 0; i < 6; i++)
                s = String.Concat(s, random.Next(10).ToString());
            return s;
        }

        public static string GenerateRandomCaptcha()
        {
            Random rnd = new Random();
            int val;
            string outstr = string.Empty;
            for (int i = 0; i < 6; ++i)
            {
                val = rnd.Next(0, 9);
                outstr += (char)('0' + val);
            }
            return outstr.Trim();
        }
        #endregion

        #region Ghi log
        public static void WriteLog(string root, string path, string msg)
        {
            if (root.Trim() != "")
            {
                StreamWriter sw;
                string sFullName = string.Format("{0:dd-MM-yyyy}.log", DateTime.Now);
                string sDirName = string.Empty;
                if (path.Trim() != "")
                    sDirName = root + path + "\\" + string.Format("{0:yyyy-MM}", DateTime.Now);
                else
                    sDirName = root + "log" + "\\" + string.Format("{0:yyyy-MM}", DateTime.Now);

                if (!Directory.Exists(sDirName + "\\"))
                    Directory.CreateDirectory(sDirName + "\\");
                sFullName = sDirName + "\\" + sFullName;
                if (File.Exists(sFullName))
                {
                    sw = System.IO.File.AppendText(sFullName);
                    sw.WriteLine(string.Format("{0:hh:mm:ss tt}", DateTime.Now) + ": " + msg);
                    sw.Flush();
                }
                else
                {
                    sw = new StreamWriter(sFullName);
                    sw.WriteLine(string.Format("{0:hh:mm:ss tt}", DateTime.Now) + ": " + msg);
                }

                if (sw != null) sw.Close();
            }
        }

        public static void Log(string root, string path, string msg)
        {
            if (path.Trim() != "")
            {
                StreamWriter sw;
                string sFullName = string.Format("{0:dd-MM-yyyy}.log", DateTime.Now);
                string sDirName = root + path + "\\" + string.Format("{0:yyyy-MM}", DateTime.Now);

                if (!Directory.Exists(sDirName + "\\"))
                    Directory.CreateDirectory(sDirName + "\\");
                sFullName = sDirName + "\\" + sFullName;
                if (File.Exists(sFullName))
                {
                    sw = System.IO.File.AppendText(sFullName);
                    sw.WriteLine(string.Format("{0:hh:mm:ss tt}", DateTime.Now) + ": " + msg);
                    sw.Flush();
                }
                else
                {
                    sw = new StreamWriter(sFullName);
                    sw.WriteLine(string.Format("{0:hh:mm:ss tt}", DateTime.Now) + ": " + msg);
                }

                if (sw != null) sw.Close();
            }
        }
        #endregion

        #region FilterData
        public static DataTable GetFilterData(DataTable table, string cond, string orderBy)
        {
            DataTable dataTable = new DataTable();
            if (table != null)
            {
                DataView dataView = new DataView(table);
                dataView.RowFilter = cond;
                if (!string.IsNullOrEmpty(orderBy))
                    dataView.Sort = orderBy;
                dataTable = dataView.ToTable();
            }
            return dataTable;
        }
        #endregion

        #region ConvertDatetime
        public static string ConvertToFormatDateTime(string sDateTime)
        {
            string sYear = sDateTime.Substring(0, 4);
            string sMonth = sDateTime.Substring(4, 2);
            string sDay = sDateTime.Substring(6, 2);
            string sHour = sDateTime.Substring(8, 2);
            string sMinute = sDateTime.Substring(10, 2);
            string sSecond = sDateTime.Substring(12, 2);
            return sDay + "-" + sMonth + "-" + sYear + " " + sHour + ":" + sMinute + ":" + sSecond;
        }

        public static string ConvertToFormatDate(string sDate)
        {
            string sYear = sDate.Substring(6, 4);
            string sMonth = sDate.Substring(3, 2);
            string sDay = sDate.Substring(0, 2);
            return sMonth + "/" + sDay + "/" + sYear;
        }

        public static DateTime ConvertToRealFormatDateTime(string sDateTime)
        {
            string sYear = sDateTime.Substring(0, 4);
            string sMonth = sDateTime.Substring(4, 2);
            string sDay = sDateTime.Substring(6, 2);
            string sHour = sDateTime.Substring(8, 2);
            string sMinute = sDateTime.Substring(10, 2);
            string sSecond = sDateTime.Substring(12, 2);
            return DateTime.Parse(sMonth + "-" + sDay + "-" + sYear + " " + sHour + ":" + sMinute + ":" + sSecond);
        }

        public static DateTime ConvertToFormatHour(string sDateTime)
        {
            string sHour = sDateTime.Substring(8, 2);
            string sMinute = sDateTime.Substring(10, 2);
            return DateTime.Parse(sHour + ":" + sMinute);
        }

        public static DateTime FormatDateTimeYMD(string sDateTime)
        {
            sDateTime = sDateTime.Replace("/", "");
            string sYear = sDateTime.Substring(4, 4);
            string sMonth = sDateTime.Substring(2, 2);
            string sDay = sDateTime.Substring(0, 2);
            return DateTime.Parse(sYear + "-" + sMonth + "-" + sDay);// + " " + sHour + ":" + sMinute + ":" + sSecond);
        }
        public static string FormatDateTimeDMY(string sDateTime)
        {
            sDateTime = sDateTime.Replace("/", "");
            string sYear = sDateTime.Substring(4, 4);
            string sMonth = sDateTime.Substring(2, 2);
            string sDay = sDateTime.Substring(0, 2);
            return sDay + "-" + sMonth + "-" + sYear;
        }

        public static DateTime FormatDateTimeYMDHMS(string sDateTime)
        {
            sDateTime = sDateTime.Replace("/", "");
            string sYear = sDateTime.Substring(4, 4);
            string sMonth = sDateTime.Substring(2, 2);
            string sDay = sDateTime.Substring(0, 2);
            string sHour = sDateTime.Substring(8, 2);
            string sMinute = sDateTime.Substring(10, 2);
            string sSecond = sDateTime.Substring(12, 2);
            return DateTime.Parse(sYear + "-" + sMonth + "-" + sDay + " " + sHour + ":" + sMinute + ":" + sSecond);
        }

        public static string ConvertFormatString(string sDateTime)
        {
            sDateTime = sDateTime.Replace("-", String.Empty).Replace(":", String.Empty).Replace(" ", String.Empty);
            string sYear = sDateTime.Substring(4, 4);
            string sMonth = sDateTime.Substring(2, 2);
            string sDay = sDateTime.Substring(0, 2);
            string sHour = sDateTime.Substring(8, 2);
            string sMinute = sDateTime.Substring(10, 2);
            string sSecond = sDateTime.Substring(12, 2);
            return sYear + sMonth + sDay + sHour + sMinute + sSecond;
        }
        #endregion

        #region Xử lý chuỗi
        //convert unicode sang dạng none Unicode
        public static string Change_AV(string ip_str_change)
        {
            Regex v_reg_regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
            string v_str_FormD = ip_str_change.Normalize(NormalizationForm.FormD);
            return v_reg_regex.Replace(v_str_FormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

        public static string EncryptMd5(string YourString)
        {
            byte[] MyByte = new byte[16];
            MyByte = MD5.Create().ComputeHash(Encoding.Default.GetBytes(YourString));
            string MyEncryptString = string.Empty;
            for (int i = 0; i < 16; i++)
            {
                MyEncryptString += MyByte[i].ToString("x2");
            }
            return MyEncryptString;
        }

        public static string CutStr(int index, string originalStr)
        {
            string reVal = originalStr;
            if (originalStr.Length > index)
            {
                reVal = originalStr.Replace(originalStr.Substring(index, originalStr.Length), " ... ");
            }
            return reVal;
        }

        public static System.Boolean IsNumeric(System.Object Expression)
        {
            if (Expression == null || Expression is DateTime)
                return false;

            if (Expression is Int16 || Expression is Int32 || Expression is Int64 || Expression is Decimal || Expression is Single || Expression is Double || Expression is Boolean || Expression is float)
                return true;

            try
            {
                if (Expression is string)
                {
                    string TempEx = Expression.ToString();
                    Double.Parse(TempEx.Replace(".", ""));
                }
                else
                    Double.Parse(Expression.ToString());
                return true;
            }
            catch { } // just dismiss errors but return false
            return false;
        }

        public static bool checkEmail(string Email)
        {
            string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" +
               @"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" +
               @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
            System.Text.RegularExpressions.Match match =
                Regex.Match(Email, pattern, RegexOptions.IgnoreCase);

            if (match.Success)
                return true;
            else
                return false;
        }

        public static string AutoID()
        {
            string str;
            try
            {
                int result = Environment.TickCount & Int32.MaxValue;
                str = result.ToString("######");

            }
            catch (Exception ex)
            {
                return null;
            }
            return str;
        }

        #endregion

        #region RemoveHTML tag

        public static string removeHTML(string orgText)
        {
            if (string.IsNullOrEmpty(orgText))
                return string.Empty;
            for (int i = 33; i < 48; i++)
            {
                orgText = orgText.Replace(((char)i).ToString(CultureInfo.InvariantCulture), "");
            }
            for (int i = 58; i < 65; i++)
            {
                orgText = orgText.Replace(((char)i).ToString(CultureInfo.InvariantCulture), "");
            }
            for (int i = 91; i < 97; i++)
            {
                orgText = orgText.Replace(((char)i).ToString(CultureInfo.InvariantCulture), "");
            }

            for (int i = 123; i < 127; i++)
            {
                orgText = orgText.Replace(((char)i).ToString(CultureInfo.InvariantCulture), "");
            }

            orgText = orgText.Replace(" ", "-");

            var regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");

            var strFormD = orgText.Normalize(System.Text.NormalizationForm.FormD).ToLower();

            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');

            //return System.Text.RegularExpressions.Regex.Replace(orgText, @"[^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]", string.Empty);
        }

        public static string RemoveHtmlTag(string str)
        {
            return Regex.Replace(str, "<.*?>", string.Empty);
        }
        #endregion

        #region trả về thứ trong tuần
        public static string ReturnThu(DayOfWeek dow)
        {

            switch (dow)
            {
                case DayOfWeek.Monday:
                    return "2";
                    break;
                case DayOfWeek.Tuesday:
                    return "3";
                    break;
                case DayOfWeek.Wednesday:
                    return "4";
                    break;
                case DayOfWeek.Thursday:
                    return "5";
                    break;
                case DayOfWeek.Friday:
                    return "6";
                    break;
                case DayOfWeek.Saturday:
                    return "7";
                    break;
                case DayOfWeek.Sunday:
                    return "chu nhat";
                    break;
                default:
                    return "";
                    break;
            }
        }
        #endregion

        #region UpLoadFile

        //Get Path + fileName has save file
        public static UploadFileClass GetUploadFile(string MediaPath, string strFileName, bool AddDatePath)
        {
            UploadFileClass tempUploadClass = new UploadFileClass();
            System.Text.RegularExpressions.Match matchResults;
            string strAdditionFolder = (AddDatePath ? String.Format(DateTime.Now.ToString("\\\\yyyy\\\\MM\\\\dd\\\\")) : "");
            string strSaveFile = strFileName;
            string strSaveFolder = MediaPath + strAdditionFolder;
            //Check folder exist
            try
            {
                if (System.IO.Directory.Exists(strSaveFolder) == false)
                {
                    //Create Directory
                    System.IO.Directory.CreateDirectory(strSaveFolder);
                }
                while (System.IO.File.Exists(strSaveFolder + strSaveFile))
                {
                    matchResults = System.Text.RegularExpressions.Regex.Match(strSaveFile, "(?<FileName>.*?)(?:\\((?<AutoNumber>\\d*?)\\))?\\.(?<FileType>\\w*?)(?!.)");
                    if (matchResults.Success)
                    {
                        if (matchResults.Groups["AutoNumber"].Value == string.Empty)
                        {
                            strSaveFile = matchResults.Groups["FileName"].Value + "(1)." + matchResults.Groups["FileType"].Value;
                        }
                        else
                        {
                            strSaveFile = matchResults.Groups["FileName"].Value + "(" + (int.Parse(matchResults.Groups["AutoNumber"].Value) + 1).ToString() + ")." + matchResults.Groups["FileType"].Value;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            tempUploadClass.Virtualpath = strAdditionFolder.Replace("\\", "/") + strSaveFile;
            tempUploadClass.FileName = strSaveFile;
            tempUploadClass.Fullpath = strSaveFolder + strSaveFile;
            tempUploadClass.FolderPath = strAdditionFolder.Replace("\\", "/");
            tempUploadClass.StrPathTemp = strSaveFolder;
            return tempUploadClass;
        }

        //resize img
        public static string ResizeImg(string FileName, string strPath, string PathUploadFile)
        {
            try
            {
                System.Drawing.Image objImage = System.Drawing.Image.FromFile(strPath);
                string fName = "Resize_" + FileName;
                UploadFileClass sSavePath = new UploadFileClass();
                sSavePath = Utils.GetUploadFile(PathUploadFile, fName, true);
                string ThumbPath = sSavePath.Fullpath;
                int sWidth = objImage.Width;
                int sHeight = objImage.Height;
                int thumbWidth = sWidth;
                int thumbHeight = sHeight;
                //if (thumbHeight > 380)
                //{
                //    thumbHeight = 380;
                //    thumbWidth = (sWidth / sHeight) * thumbHeight;
                //    //If thumbHeight > 380 Then
                //    //    thumbHeight = 380
                //    //End If
                //}
                Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
                System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
                gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
                gr.DrawImage(objImage, rectangle, 0, 0, sWidth, sHeight, GraphicsUnit.Pixel);
                bmp.Save(sSavePath.Fullpath);
                bmp.Dispose();
                return sSavePath.Virtualpath;

            }
            catch (Exception ex)
            {
            }
            finally
            {
            }

            return "";
        }
        //thumb
        public static string ThumbW120(string FileName, string strPath, string sFullPath, int lnWidth, int lnHeight)
        {
            try
            {
                System.Drawing.Image objImage = System.Drawing.Image.FromFile(sFullPath);
                string fName = "Thumb_" + FileName;
                UploadFileClass sSavePath = new UploadFileClass();
                sSavePath = Utils.GetUploadFile(strPath, fName, true);
                string ThumbPath = sSavePath.Fullpath;
                int sWidth = objImage.Width;
                int sHeight = objImage.Height;
                int thumbWidth = sWidth;
                int thumbHeight = sHeight;
                if (thumbWidth > lnWidth)
                {
                    thumbHeight = lnHeight;
                    thumbWidth = lnWidth;
                }
                Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
                System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
                gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
                gr.DrawImage(objImage, rectangle, 0, 0, sWidth, sHeight, GraphicsUnit.Pixel);
                bmp.Save(sSavePath.Fullpath);
                bmp.Dispose();
                objImage.Dispose();
                return sSavePath.FileName;

            }
            catch (Exception ex)
            {
            }
            finally
            {
            }

            return "";
        }
        #endregion

        #region Error Message
        private static string strOutputHtml = string.Empty;

        public static string Error(string caption, string message)
        {
            strOutputHtml = "    <div id=\"message-error\" class=\"message message-error\">\n";
            strOutputHtml = strOutputHtml + "       <div class=\"image\">\n";
            strOutputHtml = strOutputHtml + "           <img src=\"../../Resource/images/icons/error.png\" alt=\"" + caption + "\" height=\"32\" />\n";
            strOutputHtml = strOutputHtml + "       </div>\n";
            strOutputHtml = strOutputHtml + "       <div class=\"text\">\n";
            strOutputHtml = strOutputHtml + "           <h6>" + caption + "</h6>\n";
            strOutputHtml = strOutputHtml + "           <span>" + message + "</span>\n";
            strOutputHtml = strOutputHtml + "       </div>\n";
            strOutputHtml = strOutputHtml + "       <div class=\"dismiss\">\n";
            strOutputHtml = strOutputHtml + "           <a href=\"#message-error\"></a>\n";
            strOutputHtml = strOutputHtml + "       </div>\n";
            strOutputHtml = strOutputHtml + "   </div>\n";
            return strOutputHtml;
        }

        public static string Notice(string caption, string message)
        {
            strOutputHtml = "    <div id=\"message-notice\" class=\"message message-notice\">\n";
            strOutputHtml = strOutputHtml + "       <div class=\"image\">\n";
            strOutputHtml = strOutputHtml + "           <img src=\"../../Resource/images/icons/notice.png\" alt=\"" + caption + "\" height=\"32\" />\n";
            strOutputHtml = strOutputHtml + "       </div>\n";
            strOutputHtml = strOutputHtml + "       <div class=\"text\">\n";
            strOutputHtml = strOutputHtml + "           <h6>" + caption + "</h6>\n";
            strOutputHtml = strOutputHtml + "           <span>" + message + "</span>\n";
            strOutputHtml = strOutputHtml + "       </div>\n";
            strOutputHtml = strOutputHtml + "       <div class=\"dismiss\">\n";
            strOutputHtml = strOutputHtml + "           <a href=\"#message-notice\"></a>\n";
            strOutputHtml = strOutputHtml + "       </div>\n";
            strOutputHtml = strOutputHtml + "   </div>\n";
            return strOutputHtml;
        }

        public static string Success(string caption, string message)
        {
            strOutputHtml = "    <div id=\"message-success\" class=\"message message-success\">\n";
            strOutputHtml = strOutputHtml + "       <div class=\"image\">\n";
            strOutputHtml = strOutputHtml + "           <img src=\"../../Resource/images/icons/success.png\" alt=\"" + caption + "\" height=\"32\" />\n";
            strOutputHtml = strOutputHtml + "       </div>\n";
            strOutputHtml = strOutputHtml + "       <div class=\"text\">\n";
            strOutputHtml = strOutputHtml + "           <h6>" + caption + "</h6>\n";
            strOutputHtml = strOutputHtml + "           <span>" + message + "</span>\n";
            strOutputHtml = strOutputHtml + "       </div>\n";
            strOutputHtml = strOutputHtml + "       <div class=\"dismiss\">\n";
            strOutputHtml = strOutputHtml + "           <a href=\"#message-success\"></a>\n";
            strOutputHtml = strOutputHtml + "       </div>\n";
            strOutputHtml = strOutputHtml + "   </div>\n";
            return strOutputHtml;
        }

        public static string Warning(string caption, string message)
        {
            strOutputHtml = "    <div id=\"message-warning\" class=\"message message-warning\">\n";
            strOutputHtml = strOutputHtml + "       <div class=\"image\">\n";
            strOutputHtml = strOutputHtml + "           <img src=\"../../Resource/images/icons/warning.png\" alt=\"" + caption + "\" height=\"32\" />\n";
            strOutputHtml = strOutputHtml + "       </div>\n";
            strOutputHtml = strOutputHtml + "       <div class=\"text\">\n";
            strOutputHtml = strOutputHtml + "           <h6>" + caption + "</h6>\n";
            strOutputHtml = strOutputHtml + "           <span>" + message + "</span>\n";
            strOutputHtml = strOutputHtml + "       </div>\n";
            strOutputHtml = strOutputHtml + "       <div class=\"dismiss\">\n";
            strOutputHtml = strOutputHtml + "           <a href=\"#message-warning\"></a>\n";
            strOutputHtml = strOutputHtml + "       </div>\n";
            strOutputHtml = strOutputHtml + "   </div>\n";
            return strOutputHtml;
        }
        #endregion

        #region phân trang
        [ToolboxData("<{0}:Bjzj_2sGroup_Pager runat=\"server\"></{0}:Bjzj_2sGroup_Pager>")]
        public class Bjzj_2sGroup_Pager : WebControl, IPostBackEventHandler, INamingContainer
        {
            private bool _altEnabled = true;
            private string _BACK_TO_FIRST = "Đầu";
            private string _BACK_TO_PAGE = "Trước";
            private int _currentIndex = 1;
            private bool _enableSSC = true;
            private string _FIRST = "&lt;&lt;";
            private int _firstCompactedPageCount = 10;
            private string _FROM = "từ";
            private bool _generateGoToSection = false;
            private bool _generateHiddenHyperlinks = false;
            private string _GO = "go";
            private string _GO_TO_LAST = "Cuối";
            private bool _infoCellVisible = true;
            private double _itemCount;
            private string _LAST = "&gt;&gt;";
            private int _maxSmartShortCutCount = 6;
            private string _next = "&gt;";
            private string _NEXT_TO_PAGE = "Sau";
            private int _notCompactedPageCount = 15;
            private string _OF = "/";
            private string _PAGE = "Trang";
            private int _pageCount;
            private int _pageSize = 15;
            private string _previous = "&lt;";
            private string _queryStringParameterName = "pagerControlCurrentPageIndex";
            private bool _rightToLeft = false;
            private string _SHOW_RESULT = "Xem kết quả từ ";
            private bool _showFirstLast = false;
            private string _SHOWING_RESULT = "Đang xem kết quả từ ";
            private List<int> _smartShortCutList;
            private double _sscRatio = 3.0;
            private int _sscThreshold = 30;
            private string _TO = "tới";
            private static readonly object EventCommand = new object();

            public event CommandEventHandler Command
            {
                add
                {
                    base.Events.AddHandler(EventCommand, value);
                }
                remove
                {
                    base.Events.RemoveHandler(EventCommand, value);
                }
            }

            private void CalculateSmartShortcutAndFillList()
            {
                this._smartShortCutList = new List<int>();
                double num = (this.PageCount * this.SmartShortCutRatio) / 100.0;
                double maxSmartShortCutCount = Math.Round(num, 0);
                if (maxSmartShortCutCount > this.MaxSmartShortCutCount)
                {
                    maxSmartShortCutCount = this.MaxSmartShortCutCount;
                }
                if (maxSmartShortCutCount == 1.0)
                {
                    maxSmartShortCutCount++;
                }
                for (int i = 1; i < (maxSmartShortCutCount + 1.0); i++)
                {
                    int item = (int)(Math.Round((double)((((this.PageCount * (100.0 / maxSmartShortCutCount)) * i) / 100.0) * 0.1), 0) * 10.0);
                    if (item >= this.PageCount)
                    {
                        break;
                    }
                    this.SmartShortCutList.Add(item);
                }
            }

            private string GenerateAltMessage(int pageNumber)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append((pageNumber == this.CurrentIndex) ? this.ShowingResultClause : this.ShowResultClause);
                builder.Append(" ");
                builder.Append((int)(((pageNumber - 1) * this.PageSize) + 1));
                builder.Append(" ");
                builder.Append(this.ToClause);
                builder.Append(" ");
                builder.Append((pageNumber == this.PageCount) ? this.ItemCount : ((double)(pageNumber * this.PageSize)));
                builder.Append(" ");
                builder.Append(this.OfClause);
                builder.Append(" ");
                builder.Append(this.ItemCount);
                return builder.ToString();
            }

            private string GetAlternativeText(int pageNumber)
            {
                return (this.GenerateToolTips ? string.Format(" title=\"{0}\"", this.GenerateAltMessage(pageNumber)) : "");
            }

            private bool IsSmartShortCutAvailable()
            {
                return ((this.GenerateSmartShortCuts && (this.SmartShortCutList != null)) && (this.SmartShortCutList.Count != 0));
            }

            protected override void LoadControlState(object state)
            {
                object[] objArray = (object[])state;
                this.CurrentIndex = (int)objArray[0];
                this.PageSize = (int)objArray[1];
            }

            protected virtual void OnCommand(CommandEventArgs e)
            {
                CommandEventHandler handler = (CommandEventHandler)base.Events[EventCommand];
                if (handler != null)
                {
                    handler(this, e);
                }
            }

            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                this.Page.RegisterRequiresControlState(this);
            }

            protected override void Render(HtmlTextWriter writer)
            {
                int num;
                if (this.Page != null)
                {
                    this.Page.VerifyRenderingInServerForm(this);
                }
                if ((this.PageCount > this.SmartShortCutThreshold) && this.GenerateSmartShortCuts)
                {
                    this.CalculateSmartShortcutAndFillList();
                }
                writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "1");
                writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "1");
                writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "PagerContainerTable");
                if (this.RTL)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Dir, "rtl");
                }
                writer.RenderBeginTag(HtmlTextWriterTag.Table);
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                if (this.GeneratePagerInfoSection)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "PagerInfoCell");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write(this.PageClause + " " + this.CurrentIndex.ToString() + this.OfClause + this.PageCount.ToString());
                    writer.RenderEndTag();
                }
                if (this.GenerateFirstLastSection && (this.CurrentIndex != 1))
                {
                    writer.Write(this.RenderFirst());
                }
                if (this.CurrentIndex != 1)
                {
                    writer.Write(this.RenderBack());
                }
                if (this.CurrentIndex < this.CompactModePageCount)
                {
                    if (this.CompactModePageCount > this.PageCount)
                    {
                        this.CompactModePageCount = this.PageCount;
                    }
                    for (num = 1; num < (this.CompactModePageCount + 1); num++)
                    {
                        if (num == this.CurrentIndex)
                        {
                            writer.Write(this.RenderCurrent());
                        }
                        else
                        {
                            writer.Write(this.RenderOther(num));
                        }
                    }
                    this.RenderSmartShortCutByCriteria(this.CompactModePageCount, true, writer);
                }
                else if ((this.CurrentIndex >= this.CompactModePageCount) && (this.CurrentIndex < this.NormalModePageCount))
                {
                    if (this.NormalModePageCount > this.PageCount)
                    {
                        this.NormalModePageCount = this.PageCount;
                    }
                    for (num = 1; num < (this.NormalModePageCount + 1); num++)
                    {
                        if (num == this.CurrentIndex)
                        {
                            writer.Write(this.RenderCurrent());
                        }
                        else
                        {
                            writer.Write(this.RenderOther(num));
                        }
                    }
                    this.RenderSmartShortCutByCriteria(this.NormalModePageCount, true, writer);
                }
                else if (this.CurrentIndex >= this.NormalModePageCount)
                {
                    int num2 = this.NormalModePageCount / 2;
                    int basePageNumber = this.CurrentIndex - num2;
                    int num4 = this.CurrentIndex + num2;
                    this.RenderSmartShortCutByCriteria(basePageNumber, false, writer);
                    for (num = basePageNumber; (num < (num4 + 1)) && (num < (this.PageCount + 1)); num++)
                    {
                        if (num == this.CurrentIndex)
                        {
                            writer.Write(this.RenderCurrent());
                        }
                        else
                        {
                            writer.Write(this.RenderOther(num));
                        }
                    }
                    if (num4 < this.PageCount)
                    {
                        this.RenderSmartShortCutByCriteria(num4, true, writer);
                    }
                }
                if (this.CurrentIndex != this.PageCount)
                {
                    writer.Write(this.RenderNext());
                }
                if (this.GenerateFirstLastSection && (this.CurrentIndex != this.PageCount))
                {
                    writer.Write(this.RenderLast());
                }
                writer.RenderEndTag();
                writer.RenderEndTag();
                if (this.GenerateHiddenHyperlinks)
                {
                    writer.Write(this.RenderHiddenDiv());
                }
            }

            private string RenderBack()
            {
                string[] strArray = new string[] { "<td class=\"page-left\"><a class=\"PagerHyperlinkStyle\" href=\"{0}\" title=\" ", this.BackToPageClause, " ", (this.CurrentIndex - 1).ToString(), "\"> ", this.PreviousClause, " </a></td>" };
                int num = this.CurrentIndex - 1;
                return string.Format(string.Concat(strArray), this.Page.ClientScript.GetPostBackClientHyperlink(this, num.ToString()));
            }

            private string RenderCurrent()
            {
                return ("<td class=\"PagerCurrentPageCell\"><span class=\"PagerHyperlinkStyle\" " + this.GetAlternativeText(this.CurrentIndex) + " ><strong style=\"color:#000;font-weight:700;\">" + this.CurrentIndex.ToString() + "</strong></span></td>");
            }

            private string RenderFirst()
            {
                return string.Format("<td class=\"PagerOtherPageCells\"><a class=\"PagerHyperlinkStyle\" href=\"{0}\" title=\" " + this.BackToFirstClause + " \"> " + this.FirstClause + " </a></td>", this.Page.ClientScript.GetPostBackClientHyperlink(this, "1"));
            }

            private string RenderHiddenDiv()
            {
                int num;
                Uri url = HttpContext.Current.Request.Url;
                bool flag = !string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["QUERY_STRING"]);
                string format = "<a href=\"{0}\">page {1}</a>";
                string str2 = "<div style=\"display:none;\">{0}</div>";
                StringBuilder builder = new StringBuilder();
                if (flag && (HttpContext.Current.Request.QueryString[this.QueryStringParameterName] != null))
                {
                    Regex regex = new Regex(this.QueryStringParameterName + @"\=\d*", RegexOptions.Singleline | RegexOptions.Compiled);
                    for (num = 0; num < this.NormalModePageCount; num++)
                    {
                        builder.Append(string.Format(format, regex.Replace(url.ToString(), this.QueryStringParameterName + "=" + (num + this.CurrentIndex)), num + this.CurrentIndex));
                    }
                }
                else
                {
                    string str3 = "";
                    for (num = 0; num < this.NormalModePageCount; num++)
                    {
                        str3 = string.Format("{0}={1}", this.QueryStringParameterName, num + this.CurrentIndex);
                        builder.Append(string.Format(format, flag ? (url.ToString() + "&" + str3) : (url.ToString() + "?" + str3), num + this.CurrentIndex));
                    }
                }
                return string.Format(str2, builder.ToString());
            }

            private string RenderLast()
            {
                return string.Format("<td class=\"PagerOtherPageCells\"><a class=\"PagerHyperlinkStyle\" href=\"{0}\" title=\" " + this.GoToLastClause + " \"> " + this.LastClause + " </a></td>", this.Page.ClientScript.GetPostBackClientHyperlink(this, this.PageCount.ToString()));
            }

            private string RenderNext()
            {
                string[] strArray = new string[] { "<td class=\"page-right\"><a class=\"PagerHyperlinkStyle\" href=\"{0}\" title=\" ", this.NextToPageClause, " ", (this.CurrentIndex + 1).ToString(), "\"> ", this.NextClause, " </a></td>" };
                int num = this.CurrentIndex + 1;
                return string.Format(string.Concat(strArray), this.Page.ClientScript.GetPostBackClientHyperlink(this, num.ToString()));
            }

            private string RenderOther(int pageNumber)
            {
                return string.Format("<td class=\"PagerOtherPageCells\"><a class=\"PagerHyperlinkStyle\" href=\"{0}\" " + this.GetAlternativeText(pageNumber) + " > " + pageNumber.ToString() + " </a></td>", this.Page.ClientScript.GetPostBackClientHyperlink(this, pageNumber.ToString()));
            }

            private void RenderSmartShortCutByCriteria(int basePageNumber, bool getRightBand, HtmlTextWriter writer)
            {
                if (this.IsSmartShortCutAvailable())
                {
                    int num2;
                    List<int> smartShortCutList = this.SmartShortCutList;
                    int num = -1;
                    if (!getRightBand)
                    {
                        if (!getRightBand)
                        {
                            for (num2 = 0; num2 < smartShortCutList.Count; num2++)
                            {
                                if (basePageNumber > smartShortCutList[num2])
                                {
                                    num = num2;
                                }
                            }
                            if (num >= 0)
                            {
                                for (num2 = 0; num2 < (num + 1); num2++)
                                {
                                    if (smartShortCutList[num2] != basePageNumber)
                                    {
                                        writer.Write(this.RenderSSC(smartShortCutList[num2]));
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        for (num2 = 0; num2 < smartShortCutList.Count; num2++)
                        {
                            if (smartShortCutList[num2] > basePageNumber)
                            {
                                num = num2;
                                break;
                            }
                        }
                        if (num >= 0)
                        {
                            for (num2 = num; num2 < smartShortCutList.Count; num2++)
                            {
                                if (smartShortCutList[num2] != basePageNumber)
                                {
                                    writer.Write(this.RenderSSC(smartShortCutList[num2]));
                                }
                            }
                        }
                    }
                }
            }

            private string RenderSSC(int pageNumber)
            {
                return string.Format("<td class=\"PagerSSCCells\"><a class=\"PagerHyperlinkStyle\" href=\"{0}\" " + this.GetAlternativeText(pageNumber) + " > " + pageNumber.ToString() + " </a></td>", this.Page.ClientScript.GetPostBackClientHyperlink(this, pageNumber.ToString()));
            }

            protected override object SaveControlState()
            {
                return new object[] { this.CurrentIndex, this.PageSize };
            }

            void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
            {
                this.OnCommand(new CommandEventArgs(this.UniqueID, Convert.ToInt32(eventArgument)));
            }

            // Properties
            [Category("Globalization")]
            public string BackToFirstClause
            {
                get
                {
                    return this._BACK_TO_FIRST;
                }
                set
                {
                    this._BACK_TO_FIRST = value;
                }
            }

            [Category("Globalization")]
            public string BackToPageClause
            {
                get
                {
                    return this._BACK_TO_PAGE;
                }
                set
                {
                    this._BACK_TO_PAGE = value;
                }
            }

            [Category("Behavioural")]
            public int CompactModePageCount
            {
                get
                {
                    return this._firstCompactedPageCount;
                }
                set
                {
                    this._firstCompactedPageCount = value;
                }
            }

            [Browsable(false)]
            public int CurrentIndex
            {
                get
                {
                    return this._currentIndex;
                }
                set
                {
                    this._currentIndex = value;
                }
            }

            [Category("Globalization")]
            public string FirstClause
            {
                get
                {
                    return this._FIRST;
                }
                set
                {
                    this._FIRST = value;
                }
            }

            [Category("Globalization")]
            public string FromClause
            {
                get
                {
                    return this._FROM;
                }
                set
                {
                    this._FROM = value;
                }
            }

            [Category("Behavioural")]
            public bool GenerateFirstLastSection
            {
                get
                {
                    return this._showFirstLast;
                }
                set
                {
                    this._showFirstLast = value;
                }
            }

            [Category("Behavioural")]
            public bool GenerateGoToSection
            {
                get
                {
                    return this._generateGoToSection;
                }
                set
                {
                    this._generateGoToSection = value;
                }
            }

            [Category("Behavioural")]
            public bool GenerateHiddenHyperlinks
            {
                get
                {
                    return this._generateHiddenHyperlinks;
                }
                set
                {
                    this._generateHiddenHyperlinks = value;
                }
            }

            [Category("Behavioural")]
            public bool GeneratePagerInfoSection
            {
                get
                {
                    return this._infoCellVisible;
                }
                set
                {
                    this._infoCellVisible = value;
                }
            }

            [Category("Behavioural")]
            public bool GenerateSmartShortCuts
            {
                get
                {
                    return this._enableSSC;
                }
                set
                {
                    this._enableSSC = value;
                }
            }

            [Category("Behavioural")]
            public bool GenerateToolTips
            {
                get
                {
                    return this._altEnabled;
                }
                set
                {
                    this._altEnabled = value;
                }
            }

            [Category("Globalization")]
            public string GoClause
            {
                get
                {
                    return this._GO;
                }
                set
                {
                    this._GO = value;
                }
            }

            [Category("Globalization")]
            public string GoToLastClause
            {
                get
                {
                    return this._GO_TO_LAST;
                }
                set
                {
                    this._GO_TO_LAST = value;
                }
            }

            [Browsable(false)]
            public double ItemCount
            {
                get
                {
                    return this._itemCount;
                }
                set
                {
                    this._itemCount = value;
                    double a = this.ItemCount / ((double)this.PageSize);
                    double num2 = Math.Ceiling(a);
                    this.PageCount = Convert.ToInt32(num2);
                }
            }

            [Category("Globalization")]
            public string LastClause
            {
                get
                {
                    return this._LAST;
                }
                set
                {
                    this._LAST = value;
                }
            }

            [Category("Behavioural")]
            public int MaxSmartShortCutCount
            {
                get
                {
                    return this._maxSmartShortCutCount;
                }
                set
                {
                    this._maxSmartShortCutCount = value;
                }
            }

            [Category("Globalization")]
            public string NextClause
            {
                get
                {
                    return this._next;
                }
                set
                {
                    this._next = value;
                }
            }

            [Category("Globalization")]
            public string NextToPageClause
            {
                get
                {
                    return this._NEXT_TO_PAGE;
                }
                set
                {
                    this._NEXT_TO_PAGE = value;
                }
            }

            [Category("Behavioural")]
            public int NormalModePageCount
            {
                get
                {
                    return this._notCompactedPageCount;
                }
                set
                {
                    this._notCompactedPageCount = value;
                }
            }

            [Category("Globalization")]
            public string OfClause
            {
                get
                {
                    return this._OF;
                }
                set
                {
                    this._OF = value;
                }
            }

            [Category("Globalization")]
            public string PageClause
            {
                get
                {
                    return this._PAGE;
                }
                set
                {
                    this._PAGE = value;
                }
            }

            [Browsable(false)]
            private int PageCount
            {
                get
                {
                    return this._pageCount;
                }
                set
                {
                    this._pageCount = value;
                }
            }

            [Category("Behavioural")]
            public int PageSize
            {
                get
                {
                    return this._pageSize;
                }
                set
                {
                    this._pageSize = value;
                }
            }

            [Category("Globalization")]
            public string PreviousClause
            {
                get
                {
                    return this._previous;
                }
                set
                {
                    this._previous = value;
                }
            }

            [Category("Behavioural")]
            public string QueryStringParameterName
            {
                get
                {
                    return this._queryStringParameterName;
                }
                set
                {
                    this._queryStringParameterName = value;
                }
            }

            [Category("Globalization")]
            public bool RTL
            {
                get
                {
                    return this._rightToLeft;
                }
                set
                {
                    this._rightToLeft = value;
                }
            }

            [Category("Globalization")]
            public string ShowingResultClause
            {
                get
                {
                    return this._SHOWING_RESULT;
                }
                set
                {
                    this._SHOWING_RESULT = value;
                }
            }

            [Category("Globalization")]
            public string ShowResultClause
            {
                get
                {
                    return this._SHOW_RESULT;
                }
                set
                {
                    this._SHOW_RESULT = value;
                }
            }

            private List<int> SmartShortCutList
            {
                get
                {
                    return this._smartShortCutList;
                }
                set
                {
                    this._smartShortCutList = value;
                }
            }

            [Category("Behavioural")]
            public double SmartShortCutRatio
            {
                get
                {
                    return this._sscRatio;
                }
                set
                {
                    this._sscRatio = value;
                }
            }

            [Category("Behavioural")]
            public int SmartShortCutThreshold
            {
                get
                {
                    return this._sscThreshold;
                }
                set
                {
                    this._sscThreshold = value;
                }
            }

            [Category("Globalization")]
            public string ToClause
            {
                get
                {
                    return this._TO;
                }
                set
                {
                    this._TO = value;
                }
            }
        }
        #endregion

        #region UnSign
        public static string ConvertToUnSign(string text)
        {
            for (int i = 32; i < 48; i++)
            {

                text = text.Replace(((char)i).ToString(), " ");

            }

            text = text.Replace(".", "-");

            text = text.Replace(" ", "-");

            text = text.Replace(",", "-");

            text = text.Replace(";", "-");

            text = text.Replace(":", "-");



            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\p{IsCombiningDiacriticalMarks}+");



            string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);

            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');

        }
        #endregion
    }
}
